<?php

require_once ('system\core\Model.php');

class caseinfoModel extends CI_Model {
	
	function __construct() {
		parent::__construct ();
		$this->load->library('session'); 
		$this->load->database();
	}
	function addcaseinfo($uid,$incident_time,$court_date
			,$prosecutor,$defendant
			,$judge_name,$case_name,$case_content,$case_type,$grade)
	{
		$sql = "insert into caseinfo values(?,?,?,?,?,?,?,?,?,?,?,?)"; 
		$this->db->query($sql, array(null,$uid,$incident_time,$court_date
			,$prosecutor,$defendant,0
			,$judge_name,$case_name,$case_content,$case_type,$grade)); 
		
		
		return $this->db->affected_rows();
				
	}
	function updatecaseinfo($id,$uid,$incident_time,$court_date
			,$prosecutor,$defendant
			,$judge_name,$case_name,$case_content,$case_type)
	{
		$sql = "update caseinfo set incident_time=?,court_date=?,prosecutor=?,defendant=?,judge_name=?,case_name=?,case_content=?,case_type=? where id=?"; 
		$this->db->query($sql, array($incident_time,$court_date
			,$prosecutor,$defendant
			,$judge_name,$case_name,$case_content,$case_type,$id)); 
		return $this->db->affected_rows();
				
	}
	function getcaseinfodetail($id)
	{
		$sql="select * from caseinfo where id=?";
		$query = $this->db->query($sql, array($id));
		return $query->result_array();
	}
	function delete($id)
	{
		$sql="delete from caseinfo where id=?";
		$query = $this->db->query($sql, array($id));
		return $this->db->affected_rows();
	}
	function getcaseinfo()
	{
		return $this->db->count_all('caseinfo');  
	}
	function getcaselist($start,$size)
	{
		$sql="select * from caseinfo limit ?,?";
		$query = $this->db->query($sql, array($start,$size));
		return $query;
	}
	
	function getcaseinfoByCond($case_name
	,$case_type	
	,$incident_time_begin
	,$incident_time_end
	,$court_date_begin
	,$court_date_end
	,$prosecutor
	,$defendant)
	{
		
		$sql="select count(*) as cnt from caseinfo where case_type=? "
		 ." and case_name like ? "
		 ." and defendant like ? and prosecutor like ? "
		 ." and (court_date >= ? or '' = ? ) "
		 ." and (court_date <= ? or '' = ? ) "
		 ." and (incident_time >= ? or '' = ? ) "
		 ." and (incident_time <= ? or '' = ? ) ";
		$query = $this->db->query($sql, array($case_type
			,"%".$case_name."%"
			,"%".$defendant."%"
			,"%".$prosecutor."%"
			,$court_date_begin
			,$court_date_begin
			,$court_date_end
			,$court_date_end
			,$incident_time_begin
			,$incident_time_begin
			,$incident_time_end
			,$incident_time_end));
		 $array= $query->result_array();
		return $array[0]["cnt"];
	}
	function getcaselistByCond($start,$size,$case_name
		,$case_type
		,$incident_time_begin
		,$incident_time_end
		,$court_date_begin
		,$court_date_end
		,$prosecutor
		,$defendant)
	{
		$sql="select * from caseinfo where case_type=? "
		 ." and case_name like ? "
		 ." and defendant like ? and prosecutor like ? "
		 ." and (court_date >= ? or '' = ? ) "
		 ." and (court_date <= ? or '' = ? ) "
		 ." and (incident_time >= ? or '' = ? ) "
		 ." and (incident_time <= ? or '' = ? ) "
		 ." limit ?,?";
		$query = $this->db->query($sql, array($case_type
			,"%".$case_name."%"
			,"%".$defendant."%"
			,"%".$prosecutor."%"
			,$court_date_begin
			,$court_date_begin
			,$court_date_end
			,$court_date_end
			,$incident_time_begin
			,$incident_time_begin
			,$incident_time_end
			,$incident_time_end
			,$start
			,$size
			));
		return $query;
		
	}
}

?>